﻿using System.Threading.Tasks;
using Game.Utilities.Chat;
 
using Game.Utilities.Session;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
using Web.Server.Controllers;


namespace Web.Server.Hubs
{
    [HubName("gameHub")]
    public class PortalHub:Hub
    {
        public void Send(string mesage)
        {
            if (AccountSession.AccountID<1)
                return;

            if (string.IsNullOrEmpty(mesage.Trim()))
                return;
            var obj = GameLogHandler.Instance.TryAddMessage(AccountSession.AccountName, mesage);
            if(obj!=null)
                Clients.All.PublishMessage(obj);

        }

        public object JoinPortal()
        {
            return new { messages = GameLogHandler.Instance.ChatMessages, award = GameLogHandler.Instance.Awards ,isLogin =AccountSession.AccountID>0 };
        }

        public override Task OnConnected()
        {
 
            // Add your own code here.
            // For example: in a chat application, record the association between
            // the current connection ID and user name, and mark the user as online.
            // After the code in this method completes, the client is informed that
            // the connection is established; for example, in a JavaScript client,
            // the start().done callback is executed.
            return base.OnConnected();
        }

        public override Task OnDisconnected()
        {
            // Add your own code here.
            // For example: in a chat application, mark the user as offline, 
            // delete the association between the current connection id and user name.
            return base.OnDisconnected();
        }

        public override Task OnReconnected()
        {
            // Add your own code here.
            // For example: in a chat application, you might have marked the
            // user as offline after a period of inactivity; in that case 
            // mark the user as online again.
            return base.OnReconnected();
        }
    }
}
